package cn.hyy.FVOnline.mapper;

import cn.hyy.FVOnline.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userMapper")
public interface UserMapper {

    @Results(id = "userMap", value = {
            @Result(property = "id", column = "id", id = true),
            @Result(property = "username", column = "username"),
            @Result(property = "hsPath", column = "hspath"),
            @Result(property = "name", column = "name"),
            @Result(property = "sex", column = "sex"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "telephone", column = "telephone"),
            @Result(property = "password", column = "password"),
            @Result(property = "integral", column = "integral"),
            @Result(property = "level", column = "level")
    })
    @Select("select * from user where telephone = #{user.telephone}")
    User findByTelephone(@Param("user") User user);

    @Insert("insert into user (telephone, password) values (#{user.telephone},#{user.password})")
    void insertForRegister(@Param("user") User user);

    @Select("select * from user where telephone = #{user.telephone} and password = #{user.password}")
    User findByTelephonePassword(@Param("user") User user);

    @Select("select * from user where id = #{user.id}")
    User findById(@Param("user") User user);

    @Update("update user set username = #{user.username}, name = #{user.name}, sex = #{user.sex}, " +
            "birthday = #{user.birthday} where id = #{user.id}")
    void updateInformation(@Param("user") User user);

    @Update("update user set password = #{user.password} where id = #{user.id}")
    void updatePassword(@Param("user") User user);

    @Update("update user set hspath = #{user.hsPath} where id = #{user.id}")
    void updateHS(@Param("user") User user);

    @Select("select * from user where telephone like #{what} and level = 0 limit #{start}, #{pageSize}")
    List<User> getUser(@Param("what")String what,@Param("start")Integer start,@Param("pageSize")Integer pageSize);

    @Select("select count(*) from user where telephone like #{what} and level = 0")
    Integer getUserTotal(@Param("what")String what);

    @Update("update user set level = 1 where id = #{id}")
    void delUser(@Param("id")Integer id);

    @Update("update user set integral = integral + #{pay} where id = #{id}")
    void addPay(@Param("pay") Integer pay,@Param("id") Integer id);
}
